package cn.tedu.mall.order.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.domain.CsmallAuthenticationInfo;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.order.mapper.OmsCartMapper;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.model.OmsCart;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OmsCartServiceImpl implements IOmsCartService {

    @Autowired
    private OmsCartMapper omsCartMapper;

    @Override
    public void addCart(CartAddDTO cartDTO) {

        Long userId = getUserId();
        //检测购物车是否有该商品
        OmsCart omsCart = omsCartMapper.selectOmsCartByUserId(userId, cartDTO.getSkuId());
        //判断omsCart是否为null
        if (omsCart == null ){
            //如果没有，则进行新增
            OmsCart newOmsCart = new OmsCart();
            BeanUtils.copyProperties(cartDTO,newOmsCart);
            newOmsCart.setUserId(userId);
            omsCartMapper.saveCart(newOmsCart);
        }else {
            //如果有，则执行更新
            omsCart.setQuantity(omsCart.getQuantity()+cartDTO.getQuantity());
            omsCartMapper.updateQuantityById(omsCart);
        }

    }

    //作业：分页查询
    @Override
    public JsonPage<CartStandardVO> listCarts(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<CartStandardVO> cartStandardVOS= omsCartMapper.selectCartsByUserId(getUserId());
        return JsonPage.restPage(new PageInfo<>(cartStandardVOS));
    }

    //根据购物车id进行删除，可以删除多个
    @Override
    public void removeCart(Long[] ids) {
        int row=omsCartMapper.deleteCartsByIds(ids);
        if ( row == 0 ){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"删除购物车失败");
        }
    }
    //可以一键删除
    @Override
    public void removeAllCarts() {
        int row=omsCartMapper.deletCartsByUserId(getUserId());
        if ( row == 0 ){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"您的购物车中没有商品");
        }
    }


    //根据用户选中的商品删除 -- 订单业务使用
    @Override
    public void removeUserCarts(OmsCart omsCart) {
        omsCartMapper.deleteCartsByUserIdAndSkuId(omsCart);
    }

    @Override
    public void updateQuantity(CartUpdateDTO cartUpdateDTO) {
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(cartUpdateDTO,omsCart);
        omsCartMapper.updateQuantityById(omsCart);

    }

    public CsmallAuthenticationInfo getUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        if (authenticationToken == null) {
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"您当前没有登陆");
        }

        CsmallAuthenticationInfo userInfo = (CsmallAuthenticationInfo) authenticationToken.getCredentials();
        return userInfo;
    }

    public Long getUserId(){
        return getUserInfo().getId();
    }
}
